﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class CandidatoCadastroPre : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["login"] == null)
            Response.Redirect("mensagens.aspx?id=2");
        else
        {
            hfInscritoPor.Value = Session["login"].ToString();
            txtCPF.Focus();
        }
    }

    protected bool CPFExiste(string cpf)
    {
        bool valida = false;
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand consultaCPF = null;
        SqlDataReader drConsultaCPF;
        try
        {
            if (conexaoIntranet != null && conexaoIntranet.State != ConnectionState.Open)
                conexaoIntranet.Open();
            string instrucao = "Select CPF FROM CamaraInscritos WHERE CPF = '" + cpf + "'";
            consultaCPF = new SqlCommand(instrucao, conexaoIntranet);
            drConsultaCPF = consultaCPF.ExecuteReader();
            if (drConsultaCPF.HasRows)
            {
                valida = true;
            }
            drConsultaCPF.Close();
        }
        finally
        {
            conexaoIntranet.Close();
        }
        return valida;
    }
    protected void btnProsseguir_Click(object sender, ImageClickEventArgs e)
    {
        if (!ClassesSiteIntranet.Validadores.ValidaCPF(txtCPF.Text.Replace("-","").Replace(".","")))
            Response.Write("<script>alert('CPF inválido!');history.go(-1);</script>");
        else
        {
            Session["cpf"] = txtCPF.Text;
            if (CPFExiste(txtCPF.Text))
                Response.Redirect("CandidatoAlteracao.aspx");
            else
                Response.Redirect("CandidatoCadastro.aspx");
        }
    }
}